package com.neighbor.neighborhooduser.controller;


import com.neighbor.neighborhooduser.pojo.*;
import com.neighbor.neighborhooduser.service.IMallOrdersService;
import com.neighbor.neighborhooduser.service.IUsersService;
import com.neighbor.neighborhooduser.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;/**
 * <p>
 * 商城兑换订单表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:13:04
 */
@RestController
@RequestMapping("/mallOrders")
public class MallOrdersController {

    @Autowired
    IMallOrdersService mallOrdersService;
    @Autowired
    UsersController usersController;
    @Autowired
    MallProductsController mallProductsController;
    @Autowired
    PointsRecordsController pointsRecordsController;
    @Autowired
    NotificationsController notificationsController;
    @Autowired
    IUsersService usersService;
    Random random = new Random();
    int number = random.nextInt(10000000) + 100000;

    // 增加订单
    @RequestMapping("addOrder")
    ResponseMsg addorder(@RequestBody MallOrders mallOrders){
        MallProducts mallProducts = (MallProducts) mallProductsController.getOneProducts(mallOrders.getProductId()).getData();
        // 订单编号
        String orderNumber = "#" + LocalDate.now() + (++number);
        System.out.println("订单编号: " + orderNumber);
        mallOrders.setOrderNumber(orderNumber);
        mallOrders.setCreatedAt(new Date());
        // 得到用户信息
        Users users = (Users) usersController.getOneUsers(mallOrders.getUserId()).getData();
        System.out.println("该订单的用户信息:" + users);
        Integer ponits = users.getPoints() - mallOrders.getPointsAmount();
        if (mallOrdersService.save(mallOrders) && ponits >= 0){
            users.setPoints(ponits);
            if (usersController.updateUser(users).getCode() == 200){
                // 设置积分记录信息
                PointsRecords pointsRecords = new PointsRecords();
                pointsRecords.setUserId(mallOrders.getUserId());
                pointsRecords.setType((byte) 2);
                pointsRecords.setAmount(mallOrders.getPointsAmount());
                pointsRecords.setSourceType((byte) 5);
                pointsRecords.setSourceId(mallOrders.getProductId());
                pointsRecords.setDescription("购买了" + mallProducts.getName() + "商品");

                // 添加积分消费记录
                System.out.println(pointsRecordsController.addPointsRecords(pointsRecords));;
                mallProducts.setStock(mallProducts.getStock() - mallOrders.getQuantity());
                // 增加订单
                System.out.println(mallProductsController.updateProducts(mallProducts));;

                Notifications notifications = new Notifications();
                // 设置 订单通知
                notifications.setUserId(users.getUserId());
                notifications.setTitle("订单生成通知");
                notifications.setContent("您购买的" + mallProducts.getName() + ", 已成功生成订单");
                notifications.setType((byte) 2);
                notifications.setRelatedId(mallOrders.getOrderId());
                System.out.println("通知生成: " + notificationsController.addOrderNotification(notifications));;

                return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "增加订单成功", mallOrders);
            }
        }
        return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR, "增加订单失败");
    }}
